**************************************************
* SUMMARY STATISTICS FROM THE ECONOMIC CENSUS
**************************************************

*cap use "/Users/rchaure1/Dropbox/APcontract_reducedform (1)/economic_census_9805.dta", clear
noi di "Economic Census: Loading Data"
use "$data/economic_census_1998", clear

noi di "Economic Census: Data Loaded"

* -- THIS SHOULD BE IN THE CLEANING FILE -- *
noi di "Economic Census: Cleaning Variables"
 
gen formal = (registered==1)
gen large = (L>=10)
gen small = (L<10)

*Manufacturing sector
drop if missing(nic_2d)
gen agri = inrange(nic_2d,0,14)
gen manf = inrange(nic_2d,15,36)
gen services = inrange(nic_2d,37,99)
gen ind = agri == 1
replace ind = 2 if manf == 1
replace ind = 3 if services == 1

gen sid_01 = int(did_01/100)
 
gen F = 1

la def lind 1 "Agri." 2 "Manf." 3 "Services"
la def lformal 0 "Informal" 1 "Formal"

la val ind lind 
la val formal lformal 

/****************************
* Summary Statistics 1
****************************
preserve
gcollapse (sum) F L , by(formal ind) fast

foreach var of varlist F L {
    egen temp = sum(`var')
    gen frac_`var' = `var'/temp 
    drop temp
}

egen temp = sum(frac_L), by(ind)
gen fracL_ind = frac_L/temp
drop temp 


drop F L 
mkmat frac_L fracL_ind, mat(T) 

frmttable using "$ecoutput/EC_sumstats.tex", statmat(T) sdec(2, 2) ///
    ctitle(" ", "Frac. Workers (Uncond.)", "Frac. Workers (Cond.)",) ///
    rtitle("Informal, Agri." \ "Informal, Manf." \ "Informal Serv." \ ///
        "Formal, Agri." \ "Formal, Manf." \ "Formal Serv.") ///
    replace tex pretext(" ")
restore 


****************************
* Summary Statistics 2
****************************
gen dum = L>=100
gcollapse (sum) F L , by(ind dum) fast
foreach var of varlist F L {
    egen temp = sum(`var'), by(ind)
    gen frac_`var' = `var'/temp 
    drop temp
}
*/

****************************
* FIGURE A2 IN THE APPENDIX
****************************
preserve
gen treat = state_id == 2
collapse (sum) F L , by(formal ind treat) 

foreach var of varlist F L {
    egen temp = sum(`var')
    gen frac_`var' = `var'/temp 
    drop temp
}

egen temp = sum(frac_L), by(ind treat)
gen fracL_ind = frac_L/temp
drop temp 

egen temp = sum(frac_F), by(ind treat)
gen fracF_ind = frac_F/temp
drop F L temp 

la def ltreat 0 "Control" 1 "A.P.", modify
la val treat ltreat

graph bar (mean) fracF_ind fracL_ind if formal == 0, over(treat) over(ind) ///
    bar(1, fcolor(gs12) lcolor(black)) ///
    bar(2, fcolor(gs6) lcolor(gs6)) ///
    ylabel(, format(%3.1f)) ///
    ytitle("Fraction of workers and firms") ///
    legend(position(6) rows(1) label(1 "Firms") label(2 "Workers")) 
graph export "${output}/ec/FigureA2.png", as(png) name("Graph") replace
